/**
 * 通知服务
 * 处理系统通知相关业务逻辑
 */

import { ElMessage, ElNotification, ElMessageBox } from 'element-plus'
import type { MessageType, NotificationType } from 'element-plus'

export class NotificationService {
  /**
   * 显示消息提示
   */
  static message(
    message: string,
    type: MessageType = 'info',
    duration: number = 3000
  ): void {
    ElMessage({
      message,
      type,
      duration,
      showClose: true
    })
  }

  /**
   * 显示成功消息
   */
  static success(message: string, duration?: number): void {
    this.message(message, 'success', duration)
  }

  /**
   * 显示错误消息
   */
  static error(message: string, duration?: number): void {
    this.message(message, 'error', duration)
  }

  /**
   * 显示警告消息
   */
  static warning(message: string, duration?: number): void {
    this.message(message, 'warning', duration)
  }

  /**
   * 显示信息消息
   */
  static info(message: string, duration?: number): void {
    this.message(message, 'info', duration)
  }

  /**
   * 显示通知
   */
  static notification(
    title: string,
    message: string,
    type: NotificationType = 'info',
    duration: number = 4500
  ): void {
    ElNotification({
      title,
      message,
      type,
      duration,
      position: 'top-right'
    })
  }

  /**
   * 显示成功通知
   */
  static successNotification(title: string, message: string, duration?: number): void {
    this.notification(title, message, 'success', duration)
  }

  /**
   * 显示错误通知
   */
  static errorNotification(title: string, message: string, duration?: number): void {
    this.notification(title, message, 'error', duration)
  }

  /**
   * 显示警告通知
   */
  static warningNotification(title: string, message: string, duration?: number): void {
    this.notification(title, message, 'warning', duration)
  }

  /**
   * 显示信息通知
   */
  static infoNotification(title: string, message: string, duration?: number): void {
    this.notification(title, message, 'info', duration)
  }

  /**
   * 显示确认对话框
   */
  static async confirm(
    message: string,
    title: string = '确认',
    type: 'warning' | 'info' | 'success' | 'error' = 'warning'
  ): Promise<boolean> {
    try {
      await ElMessageBox.confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type,
        dangerouslyUseHTMLString: false
      })
      return true
    } catch {
      return false
    }
  }

  /**
   * 显示删除确认对话框
   */
  static async confirmDelete(itemName: string = '该项'): Promise<boolean> {
    return this.confirm(
      `确定要删除${itemName}吗？删除后不可恢复！`,
      '删除确认',
      'warning'
    )
  }

  /**
   * 显示批量删除确认对话框
   */
  static async confirmBatchDelete(count: number, itemName: string = '项'): Promise<boolean> {
    return this.confirm(
      `确定要删除选中的 ${count} ${itemName} 吗？删除后不可恢复！`,
      '批量删除确认',
      'warning'
    )
  }

  /**
   * 显示输入对话框
   */
  static async prompt(
    message: string,
    title: string = '输入',
    inputValue: string = '',
    inputValidator?: (value: string) => boolean | string
  ): Promise<string | null> {
    try {
      const { value } = await ElMessageBox.prompt(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue,
        inputValidator
      })
      return value
    } catch {
      return null
    }
  }

  /**
   * 显示加载消息
   */
  static loading(message: string = '加载中...'): () => void {
    const loadingInstance = ElMessage({
      message,
      type: 'info',
      duration: 0,
      showClose: false
    })

    return () => {
      loadingInstance.close()
    }
  }

  /**
   * 显示操作成功提示
   */
  static operationSuccess(operation: string = '操作'): void {
    this.success(`${operation}成功`)
  }

  /**
   * 显示操作失败提示
   */
  static operationError(operation: string = '操作', error?: string): void {
    this.error(`${operation}失败${error ? `: ${error}` : ''}`)
  }

  /**
   * 显示网络错误提示
   */
  static networkError(): void {
    this.error('网络连接失败，请检查网络设置')
  }

  /**
   * 显示权限不足提示
   */
  static permissionDenied(): void {
    this.warning('权限不足，无法执行此操作')
  }

  /**
   * 显示登录过期提示
   */
  static loginExpired(): void {
    this.warning('登录已过期，请重新登录')
  }

  /**
   * 显示数据加载失败提示
   */
  static dataLoadError(): void {
    this.error('数据加载失败，请刷新页面重试')
  }

  /**
   * 显示保存成功提示
   */
  static saveSuccess(): void {
    this.success('保存成功')
  }

  /**
   * 显示保存失败提示
   */
  static saveError(error?: string): void {
    this.error(`保存失败${error ? `: ${error}` : ''}`)
  }

  /**
   * 显示删除成功提示
   */
  static deleteSuccess(): void {
    this.success('删除成功')
  }

  /**
   * 显示删除失败提示
   */
  static deleteError(error?: string): void {
    this.error(`删除失败${error ? `: ${error}` : ''}`)
  }
}
